package com.itheima.mp.domain.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author zhangbaiyan
 * @date 2024-09-11 18:57
 * @description:
 * @version:
 */
@Data
@ApiModel(description = "分页查询条件实体")
public class PageQuery {
    @ApiModelProperty("页码")
    private Integer pageNo = 1;
    @ApiModelProperty("每页大小")
    private Integer pageSize = 5;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private String isAsc = "true";

    public <T> Page<T> toMpPage(OrderItem... items) {
        //1.1. 分页条件
        Page<T> page = Page.of(pageNo, pageSize);
        //1.2 排序条件
        if (StrUtil.isNotBlank(sortBy)) {
            //不为空
            page.addOrder(new OrderItem().setColumn(sortBy).setAsc(Boolean.parseBoolean(isAsc)));
        } else if (items != null) {
            // 为空,默认按照更新时间排序
            page.addOrder(items);

        }
        return page;

    }

    public <T> Page<T> toMpPage(String defaultSortBy, boolean defaultAsc) {
        return toMpPage(new OrderItem().setColumn(defaultSortBy).setAsc(defaultAsc));
    }
    public <T> Page<T> toMpPageDefaultSortCreateTime() {
        return toMpPage(new OrderItem().setColumn("create_time").setAsc(false));
    }
    public <T> Page<T> toMpPageDefaultSortUpdateTime() {
        return toMpPage(new OrderItem().setColumn("update_time").setAsc(false));
    }
}
